#coding: utf-8
from django.shortcuts import render_to_response

from saveontimejob import Svae_Ontime_Job
from preview import preview
from sendgroupmail import Send_Group_Mail, format_from_gdata, format_from_locad
#from make_mime_type import upload_file


 
from google.appengine.ext.webapp.util import login_required

from google.appengine.api import users


def index(request):
     
    user = users.GetCurrentUser()
    if not user:
        greeting = ("<a href=\"%s\">Sign in Google mail or register</a>." % 
                    users.create_login_url("/"))
        return render_to_response('msg.html', {'msg':greeting, 'url':'/'})
    
    
    action = request.GET.get('action', 'smail')
    
    authInfo ={}
    authInfo['server'] = ''
    authInfo['user'] = request.POST.get('sender', '') 
    authInfo['password'] = ''

    Reply_to = request.POST.get('reply_to', '')#.encode("UTF-8")
    isUsed_gdata = request.POST.get('chkGoogle', '')
    Macro = request.POST.get('txtMacroList', '')#.decode("UTF-8")
    MailTitle = request.POST.get('txtMailSubject', '')#.encode("UTF-8")
    MailContext = request.POST.get('fck_mail_body', '')#.encode("UTF-8")
    isSendMyself = request.POST.get('chkSendmyself', False) #是否发送给自己
    nMailCol = int(request.POST.get('mainIndex', 0)) - 1 #邮箱地址列
    isSendOntime = request.POST.get('checkOnTime', False)
    SendingTime = request.POST.get('txtDatetime', '')
    gdataInfo ={'gLogMail':'', 'gPsw':'', 'gFileName':'', 'wksht_name':'1'} #登录google参数
    ToMail = request.POST.get('txtSendList', '')#.decode("").encode("UTF-8")
    

    if isUsed_gdata: #判断是否使用google的数据
        gdataInfo['gLogMail'] = gLogMail = request.POST.get('gLogMail', '')
        gdataInfo['gPsw'] = gPsw = request.POST.get('gPsw', '')
        gdataInfo['gFileName'] = fileName = request.POST.get('gSheetName', '').strip()
        gdataInfo['wksht_name'] = wksht_name = request.POST.get('wksht_name', '').strip()
        listMail, errorMsg = format_from_gdata(gLogMail, gPsw, fileName, wksht_name)
    else:
        listMail, errorMsg = format_from_locad(ToMail)
    url = '/'
    if listMail == None:# 收件列表的参数，不正确刚返回，不能发送，不能保存定时
        return render_to_response('msg.html', {'msg': u'收件列表格式不正确: %s' % errorMsg, 'url':url})

    if action == 'preview':
        return preview(authInfo, Reply_to, isUsed_gdata, Macro, ToMail, \
                       MailTitle, MailContext, isSendMyself, nMailCol, \
                       isSendOntime, SendingTime, gdataInfo,listMail)

    AttachFilePath = '' 

# -----------------------------------------------------------------------------------

    if  isSendOntime:# 如果定时发送，
        rtMsg = Svae_Ontime_Job(Reply_to, ToMail, Macro, nMailCol,\
                                MailTitle, MailContext, SendingTime, gdataInfo, \
                                AttachFilePath, isSendMyself)
        return render_to_response('msg.html', {'msg': rtMsg, 'url':url})
    else:
        rtMsg, mailBoby = Send_Group_Mail(Reply_to, listMail, nMailCol,\
                                      Macro, MailTitle, MailContext, AttachFilePath ,isSendMyself)
        return render_to_response('msg.html', {'msg': rtMsg, 'url':url, 'mailBoby':mailBoby})
#end index()


if __name__ == '__main__':
    pass

